package com.eischet.lookup.web;

import org.springframework.beans.factory.annotation.Required;

import java.util.Map;

/**
 * Defines a type of searchable object, e.g. "problem" or "incidentworklog".
 * This is used by the web application to make sense of what it finds in the index.
 */
public class SearchableType {
    private String name;
    private String link;
    private String title;
    private SearchableCategory category;
    private SearchIndex searchIndex;

    /**
     * create an empty bean
     */
    public SearchableType() {
        // empty
    }

    /**
     * @param name the name
     */
    @Required
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param link the link format
     */
    @Required
    public void setLink(String link) {
        this.link = link;
    }

    /**
     * @return the title
     */
    public String getTitle() {
        return title;
    }

    /**
     * @param title the title
     */
    @Required
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * @param category the category this object belongs to
     */
    public void setCategory(SearchableCategory category) {
        this.category = category;
    }

    /**
     * @return the category this object belongs to
     */
    public SearchableCategory getCategory() {
        return category;
    }

    /**
     * @return a String representation of this object
     */
    @Override
    public String toString() {
        return String.format("SearchableType(name='%s', category='%s')", name, category);
    }

    /**
     * render an A HREF=... link tag for the object type, with the link format as configured in config.xml
     *
     * @param data  document data to read the link value from
     * @param body  the link's body contents
     * @return      a full A HREF tag (don't escape it, write it directly into the page!)
     */
    public String getLink(Map<String, String> data, String body) {
        return String.format("<a target='_blank' href='%s'>%s</a>", makeLink(data), body);
    }

    /**
     * render an HREF for a link to this object type
     * @param data  document data to read the link value from
     * @return      the HREF attribute value for the link
     */
    private String makeLink(Map<String, String> data) {
        String weblink = link;
        for (String key: data.keySet()) {
            String value = data.get(key);
            String search = String.format("${%s}", key);
            int startPos = weblink.indexOf(search);
            int endPos = startPos + search.length();
            if (startPos != -1) {
                weblink = weblink.substring(0, startPos) + value + weblink.substring(endPos);
            }
        }
        return searchIndex.getRemedyUrlBase() + weblink;
    }

    /**
     * connect this to a search index object
     * @param searchIndex   the search index we're working with
     */
    public void setSearchIndex(SearchIndex searchIndex) {
        this.searchIndex = searchIndex;
    }
}
